package com.zy.explore.strategy.application.service;

import com.zy.explore.strategy.infrastructure.enums.VipTypeEnum;
import com.zy.explore.strategy.interfaces.frm.VipCostFrm;
import com.zy.explore.strategy.interfaces.vo.ResultVO;
import com.zy.explore.strategy.interfaces.vo.VipRealCostVO;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.text.DecimalFormat;

/**
 * description 服务类
 *
 * @author 🇨🇳中国印🇨🇳
 * @date 2021年07月03日 01:36
 */
@Service
public class VipStrategyService {

    /**
     * 计算会员花费
     *
     * @param frm 请求参数对象
     * @return {@link ResultVO<VipRealCostVO>}
     */
    public VipRealCostVO calcCosts(VipCostFrm frm) {
        String vipType = frm.getVipType();
        String costs = frm.getCosts();
        if (StringUtils.isEmpty(costs)) {
            return VipRealCostVO.builder().vipType(vipType).realCosts("").build();
        }
        BigDecimal costMoney = new BigDecimal(costs);

        if (VipTypeEnum.NORMAL_VIP.getVipType().equals(vipType)) {
            // 执行普通会员打折计算逻辑
            return this.calcForNormalVip(costMoney);
        } else if (VipTypeEnum.SILVER_VIP.getVipType().equals(vipType)) {
            // 执行白银会员打折计算逻辑
            return this.calcForSilverVip(costMoney);
        } else if (VipTypeEnum.GOLD_VIP.getVipType().equals(vipType)) {
            // 执行黄金会员打折计算逻辑
            return this.calcForGoldVip(costMoney);
        } else if (VipTypeEnum.PLATINUM_VIP.getVipType().equals(vipType)) {
            // 执行白金会员打折计算逻辑
            return this.calcForPlatinumVip(costMoney);
        } else {
            // 不会出现此情况，不打折
            return VipRealCostVO.builder().vipType(vipType).realCosts(costs).build();
        }
    }

    /**
     * 计算普通VIP最终消费
     *
     * 当用户消费满1000 金额 普通会员 打98折
     *
     * @param costMoney 花费金钱
     * @return {@link VipRealCostVO}
     */
    private VipRealCostVO calcForNormalVip(BigDecimal costMoney) {
        String zero = "0";
        String oneThousand = "1000";
        if (costMoney.compareTo(new BigDecimal(zero)) == 0 || costMoney.compareTo(new BigDecimal(oneThousand)) < 1) {
            return VipRealCostVO.builder().vipType(VipTypeEnum.NORMAL_VIP.getVipType()).realCosts(costMoney.toString()).build();
        }
        DecimalFormat df = new DecimalFormat("#.##");
        return VipRealCostVO.builder().vipType(VipTypeEnum.NORMAL_VIP.getVipType()).realCosts(df.format(costMoney.multiply(new BigDecimal("0.98")))).build();
    }

    /**
     * 计算白银VIP最终消费
     *
     * 当用户消费满1000 金额 白银会员 优惠50元 再打9折
     *
     * @param costMoney 花费金钱
     * @return {@link VipRealCostVO}
     */
    private VipRealCostVO calcForSilverVip(BigDecimal costMoney) {
        String zero = "0";
        String oneThousand = "1000";
        if (costMoney.compareTo(new BigDecimal(zero)) == 0 || costMoney.compareTo(new BigDecimal(oneThousand)) < 1) {
            return VipRealCostVO.builder().vipType(VipTypeEnum.SILVER_VIP.getVipType()).realCosts(costMoney.toString()).build();
        }
        DecimalFormat df = new DecimalFormat("#.##");
        return VipRealCostVO.builder().vipType(VipTypeEnum.SILVER_VIP.getVipType()).realCosts(df.format((costMoney.subtract(new BigDecimal("50"))).multiply(new BigDecimal("0.9")))).build();
    }

    /**
     * 计算黄金VIP最终消费
     *
     * 当用户消费满1000 金额 黄金会员 8折
     *
     * @param costMoney 花费金钱
     * @return {@link VipRealCostVO}
     */
    private VipRealCostVO calcForGoldVip(BigDecimal costMoney) {
        String zero = "0";
        String oneThousand = "1000";
        if (costMoney.compareTo(new BigDecimal(zero)) == 0 || costMoney.compareTo(new BigDecimal(oneThousand)) < 1) {
            return VipRealCostVO.builder().vipType(VipTypeEnum.GOLD_VIP.getVipType()).realCosts(costMoney.toString()).build();
        }
        DecimalFormat df = new DecimalFormat("#.##");
        return VipRealCostVO.builder().vipType(VipTypeEnum.GOLD_VIP.getVipType()).realCosts(df.format(costMoney.multiply(new BigDecimal("0.8")))).build();
    }

    /**
     * 计算白金VIP最终消费
     *
     * 当用户消费满1000 金额 白金会员 优惠50元，再打7折
     *
     * @param costMoney 花费金钱
     * @return {@link VipRealCostVO}
     */
    private VipRealCostVO calcForPlatinumVip(BigDecimal costMoney) {
        String zero = "0";
        String oneThousand = "1000";
        if (costMoney.compareTo(new BigDecimal(zero)) == 0 || costMoney.compareTo(new BigDecimal(oneThousand)) < 1) {
            return VipRealCostVO.builder().vipType(VipTypeEnum.PLATINUM_VIP.getVipType()).realCosts(costMoney.toString()).build();
        }
        DecimalFormat df = new DecimalFormat("#.##");
        return VipRealCostVO.builder().vipType(VipTypeEnum.PLATINUM_VIP.getVipType()).realCosts(df.format((costMoney.subtract(new BigDecimal("50"))).multiply(new BigDecimal("0.7")))).build();
    }
}
